package Announce;

import java.io.IOException;
import java.io.ObjectInputStream;

import javax.swing.text.BadLocationException;

import Chat.ChatForClient;
import gui.ServerInfo;

public class ThreadNhanThongBao extends Thread {
	
	public ServerInfo si;
	public ObjectInputStream input;
	
	public ThreadNhanThongBao(ServerInfo s) {
		si = s;
		
	}

	public void run() {
		try {
			input = new ObjectInputStream(si.announceSocket.getInputStream());
		} catch (IOException e) {
			//e.printStackTrace();
			if(si.announceSocket != null && !si.announceSocket.isClosed()) {
				try {
					si.announceSocket.close();
					si.announceSocket = null;
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
			si.threadNhanTB = null;
			
			System.out.println("Thread nhan thong bao da dong!");
			return;
		}
		
		while(true) {
			String s = null;
			try {
				s = input.readUTF();
			} catch (IOException e) {
				if(si.announceSocket != null && !si.announceSocket.isClosed()) {
					try {
						si.announceSocket.close();
						si.announceSocket = null;
					} catch (IOException e1) {
						e1.printStackTrace();
					}
				}
				si.threadNhanTB = null;
				
				System.out.println("Thread nhan thong bao da dong!");
				break;
			}
			if(!s.isEmpty())  {
				ThongBaoForm tbf = new ThongBaoForm();
				tbf.txtThgBao.setText(s);
				tbf.setVisible(true);
				
//				if(si.tbform == null) {
//					si.tbform = new ThongBaoForm();
//					si.tbform.setVisible(true);
//					si.tbform.txtThgBao.setText(s);
//					
//				}
//				else 				
//				{
//					si.tbform.dispose();
//					si.tbform = new ThongBaoForm();
//					si.tbform.setVisible(true);
//					si.tbform.txtThgBao.setText(s);
//				}
//				
			}
		}
	}
	
	

}
